package courseScheduler;

import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import javax.swing.JTextField;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJPanel.java
 *
 * Created on Apr 14, 2011, 2:09:58 PM
 */

/**
 *
 * @author lyn5023
 */
public class LoginPanelOne extends SpecialContentPanel  {
    /** Creates new form NewJPanel */
    public LoginPanelOne(MainThread newParent) {
        initComponents();
        parent = newParent;
        Config configFile=new Config();
        String[] args=new String[3];
        args=configFile.recoverConfigFile();
        if(!(configFile.recoverConfigFile()==null)){
            UserNameInput.setText(args[1]);
            PasswordInput.setText(args[2]);
            ServerAddressInput.setText(args[0]);
        }
        else{
            args=null;
            configFile=null;
            System.gc();
        }
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        UserNameInput = new javax.swing.JTextField();
        PasswordInput = new javax.swing.JPasswordField();
        OkButton = new javax.swing.JButton();
        ClearButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        ServerAddressInput = new javax.swing.JTextField();
        SaveConnectionCheckBox = new javax.swing.JCheckBox();
        UserNameLabel = new javax.swing.JLabel();
        PasswordLabel = new javax.swing.JLabel();
        ServerLabel = new javax.swing.JLabel();
        AddressLabel = new javax.swing.JLabel();
        UserLabel = new javax.swing.JLabel();

        setMaximumSize(new java.awt.Dimension(300, 300));
        setMinimumSize(new java.awt.Dimension(300, 300));
        setPreferredSize(new java.awt.Dimension(300, 300));

        OkButton.setFont(new java.awt.Font("Tahoma", 1, 11));
        OkButton.setText("OK");
        OkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OkButtonActionPerformed(evt);
            }
        });

        ClearButton.setText("Clear");
        ClearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ClearButtonActionPerformed(evt);
            }
        });

        CancelButton.setText("Cancel");

        ServerAddressInput.setText("127.0.0.1");

        SaveConnectionCheckBox.setText("Save Connection");

        UserNameLabel.setText("User Name");

        PasswordLabel.setText("Password");

        ServerLabel.setText("Server Info");

        AddressLabel.setText("Address");

        UserLabel.setText("User Info");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(OkButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ClearButton, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(CancelButton))
                            .addComponent(SaveConnectionCheckBox)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(UserNameLabel)
                                    .addComponent(PasswordLabel)
                                    .addComponent(AddressLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(ServerAddressInput, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(PasswordInput)
                                        .addComponent(UserNameInput, javax.swing.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE))))))
                    .addComponent(UserLabel)
                    .addComponent(ServerLabel))
                .addGap(95, 95, 95))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(UserLabel)
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(UserNameLabel)
                    .addComponent(UserNameInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PasswordLabel)
                    .addComponent(PasswordInput, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addComponent(ServerLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(AddressLabel)
                    .addComponent(ServerAddressInput, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(SaveConnectionCheckBox)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(OkButton)
                    .addComponent(ClearButton)
                    .addComponent(CancelButton))
                .addGap(61, 61, 61))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel AddressLabel;
    private javax.swing.JButton CancelButton;
    private javax.swing.JButton ClearButton;
    private javax.swing.JButton OkButton;
    private javax.swing.JPasswordField PasswordInput;
    private javax.swing.JLabel PasswordLabel;
    private javax.swing.JCheckBox SaveConnectionCheckBox;
    private javax.swing.JTextField ServerAddressInput;
    private javax.swing.JLabel ServerLabel;
    private javax.swing.JLabel UserLabel;
    private javax.swing.JTextField UserNameInput;
    private javax.swing.JLabel UserNameLabel;
    // End of variables declaration//GEN-END:variables
    
    public final String DefaultUser = "";
    public final String DefaultPWord = "";
    public final String DefaultHost = "127.0.0.1";
    private mySQLDriver driver;


    private void ClearButtonActionPerformed(java.awt.event.ActionEvent evt){
        UserNameInput.setText(DefaultUser);
        PasswordInput.setText(DefaultPWord);
        ServerAddressInput.setText(DefaultHost);
    }

    private void OkButtonActionPerformed(java.awt.event.ActionEvent evt){
        if (SaveConnectionCheckBox.isSelected()){
            Config configFile = new Config(ServerAddressInput.getText(),
                                           UserNameInput.getText(),
                                           new String(PasswordInput.getPassword()));
            configFile.generateConfigFile();
        }
        try {
            driver = new mySQLDriver(ServerAddressInput.getText());
            
            String type = Person.getType(driver.getConnection(), UserNameInput.getText(), new String(PasswordInput.getPassword()));
            System.out.println(type);
            int id = Person.getID(driver.getConnection(), UserNameInput.getText(), new String(PasswordInput.getPassword()));
            parent.LoginFrame.setVisible(false);
            parent.logOn(driver.getConnection(), type, id);
        } catch (SchedulerException ex) {
            Logger.getLogger(LoginPanelOne.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(LoginPanelOne.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(LoginPanelOne.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(LoginPanelOne.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(LoginPanelOne.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
}
